package com.jade.explorer.ui.exp;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.jade.explorer.dirscan.FileSystem;
import com.jade.explorer.ui.SWTResourceManager;
import com.jade.explorer.views.JadeSharedImages;

import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.jface.viewers.ListViewer;

public class MultiFileExportDialog extends Dialog {
	
	private String _path;
	private String _expPath = "";
	private boolean _result = false;
	private HashMap<String, String> _road = new HashMap<String, String>();
	private ArrayList<String> _nameList = new ArrayList<String>();
	private ArrayList<String> _finalList = new ArrayList<String>();
	private Button _okButton;
	private boolean _over = false;;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public MultiFileExportDialog(Shell parentShell, String path) {
		super(parentShell);
		_path = path;
		
		File dir = new File(path);
		
		for(File f : dir.listFiles()){
			_road.put(f.getName(), f.getPath());
			_nameList.add(f.getName());
		}
		
		
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);

		//---------------------------
		Composite titleFrame = new Composite(container, SWT.NONE);
		titleFrame.setBounds(11, 13, 64, 64);
		titleFrame.setBounds(0, 0, 444, 44);
		titleFrame.setBackgroundImage(JadeSharedImages.getMenuTitleImg());
		titleFrame.setFont(SWTResourceManager.getFont("Rockwell", 14, SWT.BOLD));
		titleFrame.addPaintListener(new PaintListener() { 
			public void paintControl(PaintEvent e) { 
				e.gc.drawString("Export", 10, 10, true); 
				} 
		}); 
		//----------------------------
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setBounds(0, 44, 444, 327);
		
		ListViewer listViewer = new ListViewer(composite, SWT.BORDER | SWT.V_SCROLL);
		final List primList = listViewer.getList();
		primList.setBounds(10, 78, 170, 239);
		
		for (String s : _nameList){
			primList.add(s);
		}
		
		
		ListViewer listViewer_1 = new ListViewer(composite, SWT.BORDER | SWT.V_SCROLL);
		final List finalList = listViewer_1.getList();
		finalList.setBounds(264, 78, 170, 239);
		
		Button addButton = new Button(composite, SWT.NONE);
		addButton.setBounds(183, 129, 78, 25);
		addButton.setText("Add >");
		addButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				if (primList.getSelectionCount() > 0){
					finalList.add(primList.getItem(primList.getSelectionIndex()));
					_finalList.add(primList.getItem(primList.getSelectionIndex()));
					primList.remove(primList.getSelectionIndex());
				}
			}
		});
		
		Button addAllButton = new Button(composite, SWT.NONE);
		addAllButton.setBounds(183, 160, 78, 25);
		addAllButton.setText("Add All >");
		addAllButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				if (primList.getItemCount() > 0){
					primList.removeAll();
					finalList.removeAll();
					_finalList.clear();
					
					for (String s : _nameList){
						finalList.add(s);
						_finalList.add(s);
					}
				}
			}
		});
		
		Button delButton = new Button(composite, SWT.NONE);
		delButton.setBounds(183, 217, 78, 25);
		delButton.setText("< Delete");
		delButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
					
				if (finalList.getSelectionCount() > 0){
					primList.add(finalList.getItem(finalList.getSelectionIndex()));
					_finalList.remove(finalList.getSelectionIndex());
					finalList.remove(finalList.getSelectionIndex());
				}
			}
		});
		
		Button delAllButton = new Button(composite, SWT.NONE);
		delAllButton.setBounds(183, 248, 78, 25);
		delAllButton.setText("< Delete All");
		delAllButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				if (finalList.getItemCount() > 0){
					primList.removeAll();
					finalList.removeAll();
					_finalList.clear();
					
					for (String s : _nameList){
						primList.add(s);
					}
				}
			}
		});
		
		
		final Text pathText = new Text(composite, SWT.BORDER);
		pathText.setEditable(false);
		pathText.setBounds(10, 10, 251, 21);
		
		final Shell shell = super.getShell();
		Button pathButton = new Button(composite, SWT.NONE);
		pathButton.setBounds(267, 8, 167, 25);
		pathButton.setText("Choose path");
		pathButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				DirectoryDialog dialog = new DirectoryDialog(shell);
				String importPath = dialog.open(); 
				
				if (importPath != null){
						pathText.setText(importPath);
						_expPath = importPath;
						_okButton.setEnabled(true);
					}
				}
		});
		
		Label toExportLabel = new Label(composite, SWT.NONE);
		toExportLabel.setBounds(10, 57, 170, 15);
		toExportLabel.setText("Files in project:");
		
		Label exportedLabel = new Label(composite, SWT.NONE);
		exportedLabel.setBounds(264, 57, 170, 15);
		exportedLabel.setText("Files to export:");
		
		Button overWriteCheckButton = new Button(composite, SWT.CHECK);
		overWriteCheckButton.setBounds(10, 35, 170, 16);
		overWriteCheckButton.setText("Overwrite existing files");
		overWriteCheckButton.setSelection(false);
		overWriteCheckButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				if (_over) _over = false;
				else _over = true;
				
			}
		});

		
		
		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		final Shell shell = super.getShell();
		_okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		_okButton.setEnabled(false);
		_okButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				if (_expPath != ""){
					
					_result = true;
					
					for(String s : _finalList){
						
						if (_over){
							if (!FileSystem.copyFileToWithOverwrite(_road.get(s), _expPath)) 
								_result = false;
						}
						else{
							if (!FileSystem.copyFileToWithNoOverwrite(_road.get(s), _expPath)) 
								_result = false;
						}
					}
					
					close();
				}
				else{
					MessageDialog.openError(shell, "Error", "Choose directory for export");
					open();
				}
				

			}
		});
		
		Button cancelButton = createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
		cancelButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				_result = false;
				close();
			}
		});
		
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 450);
	}
	
	
	public boolean getResult(){
		return _result;
	}
}
